package com.honghe.system.domain;

import com.honghe.common.core.domain.BaseEntity;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.honghe.common.annotation.Excel;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 银行客户案件对象 bank_customer_cases
 * 
 * @author honghe
 */
public class BankCustomerCases extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 案件编号 */
    private String caseId;

    /** 客户姓名 */
    @Excel(name = "客户姓名")
    private String customerName;

    /** 联系电话 */
    @Excel(name = "联系电话")
    private String contactPhone;

    /** 身份证号码 */
    @Excel(name = "身份证号码")
    private String idCardNumber;

    /** 案件负责人 */
    @Excel(name = "案件负责人")
    private String caseHandler;

    /** 委托机构 */
    @Excel(name = "委托机构")
    private String entrustingInstitution;

    /** 债务类型 */
    @Excel(name = "债务类型")
    private String debtType;

    /** 诉讼/调解案号 */
    @Excel(name = "诉讼/调解案号")
    private String litigationMediationCaseNumber;

    /** 执行案号 */
    @Excel(name = "执行案号")
    private String enforcementCaseNumber;

    /** 案件进度状态 */
    @Excel(name = "案件进度状态")
    private String caseProgressStatus;

    /** 案件进度说明 */
    @Excel(name = "案件进度说明")
    private String caseProgressDescription;

    /** 文件信息 */
    private String fileInfo;

    /** 经办人 */
    @Excel(name = "经办人")
    private String handlerUser;

    /** 公海池类型 */
    @Excel(name = "公海池类型", readConverterExp = "1=大公海池,2=已判决客户池,3=终本客户池,4=已调解客户池,5=已完结客户池,6=强制执行客户池,7=已清偿客户池,8=暂时挂起")
    private String poolType;

    /** 进入公海池时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "进入公海池时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
    private Date poolEntryTime;

    /** 关联人姓名（JSON数组格式） */
    @Excel(name = "关联人姓名")
    private String contactPersonName;

    /** 关联人联系方式（JSON数组格式） */
    @Excel(name = "关联人联系方式")
    private String contactPersonPhone;
    
    /** 关联人身份证号码（JSON数组格式） */
    private String contactPersonIdCard;

    /** 关联人列表（非数据库字段，用于前端展示） */
    private List<Map<String, String>> contactPersonList;

    /** 是否在客户列表中显示 */
    private Boolean isVisible;

    /** 客户介绍 */
    @Excel(name = "客户介绍")
    private String customerIntro;

    public List<Map<String, String>> getContactPersonList() {
        if (contactPersonList == null) {
            contactPersonList = new ArrayList<>();
            // 如果已有数据，解析为List
            if (contactPersonName != null && contactPersonPhone != null) {
                try {
                    // 假设contactPersonName、contactPersonPhone和contactPersonIdCard存储的是JSON数组
                    ObjectMapper mapper = new ObjectMapper();
                    List<String> names = mapper.readValue(contactPersonName, List.class);
                    List<String> phones = mapper.readValue(contactPersonPhone, List.class);
                    List<String> idCards = new ArrayList<>();
                    
                    // 解析身份证号码，如果存在
                    if (contactPersonIdCard != null && !contactPersonIdCard.isEmpty()) {
                        try {
                            idCards = mapper.readValue(contactPersonIdCard, List.class);
                        } catch (Exception e) {
                            // 如果解析失败，创建一个空列表
                            idCards = new ArrayList<>();
                        }
                    }
                    
                    // 确保三个列表长度一致，取最大长度
                    int maxSize = Math.max(names.size(), Math.max(phones.size(), idCards.size()));
                    for (int i = 0; i < maxSize; i++) {
                        Map<String, String> person = new HashMap<>();
                        person.put("name", i < names.size() ? names.get(i) : "");
                        person.put("phone", i < phones.size() ? phones.get(i) : "");
                        person.put("idCard", i < idCards.size() ? idCards.get(i) : "");
                        contactPersonList.add(person);
                    }
                } catch (Exception e) {
                    // 如果不是JSON格式，兼容处理单个联系人的情况
                    if (contactPersonName != null && !contactPersonName.isEmpty()) {
                        Map<String, String> person = new HashMap<>();
                        person.put("name", contactPersonName);
                        person.put("phone", contactPersonPhone);
                        person.put("idCard", contactPersonIdCard);
                        contactPersonList.add(person);
                    }
                }
            }
        }
        return contactPersonList;
    }

    public void setContactPersonList(List<Map<String, String>> contactPersonList) {
        this.contactPersonList = contactPersonList;
        // 更新时同步更新JSON字符串
        if (contactPersonList != null && !contactPersonList.isEmpty()) {
            try {
                ObjectMapper mapper = new ObjectMapper();
                List<String> names = new ArrayList<>();
                List<String> phones = new ArrayList<>();
                List<String> idCards = new ArrayList<>();
                
                for (Map<String, String> person : contactPersonList) {
                    names.add(person.get("name"));
                    phones.add(person.get("phone"));
                    idCards.add(person.get("idCard"));
                }
                
                this.contactPersonName = mapper.writeValueAsString(names);
                this.contactPersonPhone = mapper.writeValueAsString(phones);
                this.contactPersonIdCard = mapper.writeValueAsString(idCards);
            } catch (Exception e) {
                // 处理转换异常
            }
        } else {
            this.contactPersonName = "[]";
            this.contactPersonPhone = "[]";
            this.contactPersonIdCard = "[]";
        }
    }

    public String getContactPersonName() {
        return contactPersonName;
    }

    public void setContactPersonName(String contactPersonName) {
        this.contactPersonName = contactPersonName;
    }

    public String getContactPersonPhone() {
        return contactPersonPhone;
    }

    public void setContactPersonPhone(String contactPersonPhone) {
        this.contactPersonPhone = contactPersonPhone;
    }

    public String getContactPersonIdCard() {
        return contactPersonIdCard;
    }

    public void setContactPersonIdCard(String contactPersonIdCard) {
        this.contactPersonIdCard = contactPersonIdCard;
    }

    public void setHandlerUser(String handlerUser) 
    {
        this.handlerUser = handlerUser;
    }

    public String getHandlerUser() 
    {
        return handlerUser;
    }

    public void setCaseId(String caseId) 
    {
        this.caseId = caseId;
    }

    public String getCaseId() 
    {
        return caseId;
    }

    public void setCustomerName(String customerName) 
    {
        this.customerName = customerName;
    }

    public String getCustomerName() 
    {
        return customerName;
    }

    public void setContactPhone(String contactPhone) 
    {
        this.contactPhone = contactPhone;
    }

    public String getContactPhone() 
    {
        return contactPhone;
    }

    public void setIdCardNumber(String idCardNumber) 
    {
        this.idCardNumber = idCardNumber;
    }

    public String getIdCardNumber() 
    {
        return idCardNumber;
    }

    public void setCaseHandler(String caseHandler) 
    {
        this.caseHandler = caseHandler;
    }

    public String getCaseHandler() 
    {
        return caseHandler;
    }

    public void setEntrustingInstitution(String entrustingInstitution) 
    {
        this.entrustingInstitution = entrustingInstitution;
    }

    public String getEntrustingInstitution() 
    {
        return entrustingInstitution;
    }

    public void setDebtType(String debtType) 
    {
        this.debtType = debtType;
    }

    public String getDebtType() 
    {
        return debtType;
    }

    public void setLitigationMediationCaseNumber(String litigationMediationCaseNumber) 
    {
        this.litigationMediationCaseNumber = litigationMediationCaseNumber;
    }

    public String getLitigationMediationCaseNumber() 
    {
        return litigationMediationCaseNumber;
    }

    public void setEnforcementCaseNumber(String enforcementCaseNumber) 
    {
        this.enforcementCaseNumber = enforcementCaseNumber;
    }

    public String getEnforcementCaseNumber() 
    {
        return enforcementCaseNumber;
    }

    public void setCaseProgressStatus(String caseProgressStatus) 
    {
        this.caseProgressStatus = caseProgressStatus;
    }

    public String getCaseProgressStatus() 
    {
        return caseProgressStatus;
    }

    public void setCaseProgressDescription(String caseProgressDescription) 
    {
        this.caseProgressDescription = caseProgressDescription;
    }

    public String getCaseProgressDescription() 
    {
        return caseProgressDescription;
    }

    public void setFileInfo(String fileInfo) 
    {
        this.fileInfo = fileInfo;
    }

    public String getFileInfo() 
    {
        return fileInfo;
    }

    public String getPoolType() {
        return poolType;
    }

    public void setPoolType(String poolType) {
        this.poolType = poolType;
    }

    public Date getPoolEntryTime() {
        return poolEntryTime;
    }

    public void setPoolEntryTime(Date poolEntryTime) {
        this.poolEntryTime = poolEntryTime;
    }

    public Boolean getIsVisible() {
        return isVisible;
    }

    public void setIsVisible(Boolean isVisible) {
        this.isVisible = isVisible;
    }

    public String getCustomerIntro() {
        return customerIntro;
    }

    public void setCustomerIntro(String customerIntro) {
        this.customerIntro = customerIntro;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
            .append("caseId", getCaseId())
            .append("customerName", getCustomerName())
            .append("contactPhone", getContactPhone())
            .append("idCardNumber", getIdCardNumber())
            .append("caseHandler", getCaseHandler())
            .append("handlerUser", getHandlerUser())
            .append("entrustingInstitution", getEntrustingInstitution())
            .append("debtType", getDebtType())
            .append("litigationMediationCaseNumber", getLitigationMediationCaseNumber())
            .append("enforcementCaseNumber", getEnforcementCaseNumber())
            .append("caseProgressStatus", getCaseProgressStatus())
            .append("caseProgressDescription", getCaseProgressDescription())
            .append("fileInfo", getFileInfo())
            .append("poolType", getPoolType())
            .append("poolEntryTime", getPoolEntryTime())
            .append("contactPersonName", getContactPersonName())
            .append("contactPersonPhone", getContactPersonPhone())
            .append("contactPersonIdCard", getContactPersonIdCard())
            .append("createBy", getCreateBy())
            .append("createTime", getCreateTime())
            .append("updateBy", getUpdateBy())
            .append("updateTime", getUpdateTime())
            .append("remark", getRemark())
            .append("isVisible", getIsVisible())
            .append("customerIntro", getCustomerIntro())
            .toString();
    }
}
